/*
 * Created by JFormDesigner on Thu Jun 27 10:47:22 CST 2024
 */

package scct.yangxianpeng.ui;

import scct.yangxianpeng.dao.UserDao;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 35403
 */
public class DlgUserAdd extends JDialog {
    public DlgUserAdd(Window owner) {
        super(owner);
        initComponents();
    }

    private void btnAdd(ActionEvent e) {
        String username = txtName.getText();
        String pass = txtPass.getText();
        int age = Integer.parseInt(txtAge.getModel().getValue().toString());
        String gender = radMale.isSelected() ? "男" : "女";
        String mobile = txtMobile.getText();
        if (!username.matches("^\\w{3,8}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的用户名");
            return;
        }
        if (!pass.matches("^\\w{3,8}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的密码");
            return;
        }
        if (age < 10 || age > 100) {
            JOptionPane.showMessageDialog(this, "请输入正确的年龄");
            return;
        }
        if (!mobile.matches("^1[3-9]\\d{9}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的手机号码");
            return;
        }
        UserDao userDao = new UserDao();
        //判断重名
        if (userDao.isDupname(username)) {
            JOptionPane.showMessageDialog(this, "你输入的用户名已存在，请重新输入！");
            return;
        }
        if (userDao.addUser(username, pass, gender, age, mobile)) {
            JOptionPane.showMessageDialog(this, "添加成功");
        }
        this.dispose();
    }

    private void btnCancel(ActionEvent e) {
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - yang
        txtName = new JTextField();
        txtPass = new JTextField();
        txtMobile = new JTextField();
        txtAge = new JSpinner();
        radMale = new JRadioButton();
        radFemale = new JRadioButton();
        btnAdd = new JButton();
        btnCancel = new JButton();
        labelName = new JLabel();
        labelPass = new JLabel();
        labelMobile = new JLabel();
        labelAge = new JLabel();
        labelGender = new JLabel();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7528\u6237");
        var contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(txtName);
        txtName.setBounds(210, 35, 160, 36);
        contentPane.add(txtPass);
        txtPass.setBounds(210, 85, 160, 36);
        contentPane.add(txtMobile);
        txtMobile.setBounds(210, 125, 160, 36);
        contentPane.add(txtAge);
        txtAge.setBounds(210, 170, 160, 36);

        //---- radMale ----
        radMale.setText("\u7537");
        radMale.setSelected(true);
        contentPane.add(radMale);
        radMale.setBounds(new Rectangle(new Point(215, 225), radMale.getPreferredSize()));

        //---- radFemale ----
        radFemale.setText("\u5973");
        contentPane.add(radFemale);
        radFemale.setBounds(new Rectangle(new Point(300, 225), radFemale.getPreferredSize()));

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(130, 270), btnAdd.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        btnCancel.addActionListener(e -> btnCancel(e));
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(300, 270), btnCancel.getPreferredSize()));

        //---- labelName ----
        labelName.setText("\u7528\u6237\u540d");
        contentPane.add(labelName);
        labelName.setBounds(125, 45, labelName.getPreferredSize().width, 18);

        //---- labelPass ----
        labelPass.setText("\u5bc6\u7801");
        contentPane.add(labelPass);
        labelPass.setBounds(125, 95, 43, 18);

        //---- labelMobile ----
        labelMobile.setText("\u624b\u673a\u53f7\u7801");
        contentPane.add(labelMobile);
        labelMobile.setBounds(125, 135, 65, 18);

        //---- labelAge ----
        labelAge.setText("\u5e74\u9f84");
        contentPane.add(labelAge);
        labelAge.setBounds(125, 180, 43, 18);

        //---- labelGender ----
        labelGender.setText("\u6027\u522b");
        contentPane.add(labelGender);
        labelGender.setBounds(125, 230, 43, 18);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(500, 375);
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMale);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yang
    private JTextField txtName;
    private JTextField txtPass;
    private JTextField txtMobile;
    private JSpinner txtAge;
    private JRadioButton radMale;
    private JRadioButton radFemale;
    private JButton btnAdd;
    private JButton btnCancel;
    private JLabel labelName;
    private JLabel labelPass;
    private JLabel labelMobile;
    private JLabel labelAge;
    private JLabel labelGender;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
